package com.gxa.hualian.controller;

import com.gxa.hualian.anno.NeedToken;
import com.gxa.hualian.exception.BusinessException;
import com.gxa.hualian.exception.ExceptionCode;
import com.gxa.hualian.pojo.Manager;
import com.gxa.hualian.pojo.User;
import com.gxa.hualian.service.ManagerService;
import com.gxa.hualian.service.UserService;
import com.gxa.hualian.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.UUID;

/**
 * @author lijunlin
 * @date 2022/5/29
 * 在controller中异常的处理方法:
 * 1. Spring 利用AOP思想(环绕通知),封装了一个处理异常的切面: @CentrollerAdvice
 * 切点:所有的控制器中的方法(Controller)
 * 通知:封装的环绕通知:@ExceptionHandler
 *
 *
 **/
@Api(value = "用户模块相关的接口")
@Controller
@RequestMapping("/user")//一级映射
@RestController
@CrossOrigin
public class UserController {
    /**
     * 泛型依赖注入只能用    @Autowired注解
     * 在BeanService中用
     */
    @Autowired
    UserService userService;

    /**
     * *1.执行登录操作,服务器生成token(UUID),并放入session
     * *2.、当业务操作需要从服务器的session中取到token的时候,将其声明在方法的参数中即可*
     * 3，前端逦过请求头传递token.和session保存的token进行校验
     * 水token多数需要添加@RequestHeader ()
     * 哀4.Swagger将该类型的参数指定为header,告知前瑙该参数可以通过authorize全局设置该请求头*校验通过才执行业务景作
     * @param id
     * @param token
     * @param session
     * @return
     * @throws Exception
     */
    @ApiOperation("根据id用户信息方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user",value = "用户token",paramType = "header")
    })

    @NeedToken
    @PostMapping("/selectById")//二级映射
    public Map selectById(@RequestParam(value = "id", required = true, defaultValue = "1") Integer id,@RequestHeader@ApiIgnore String token,@ApiIgnore HttpSession session) throws Exception {
// 因为有拦截器
//       if (StringUtils.isEmpty(token)){
//           throw new BusinessException(ExceptionCode.ENPTY_TOKEN);
//       }
//       if (!token.equals(session.getAttribute("token"))){
//           throw new BusinessException(ExceptionCode.WRONG_TOKEN);
//       }
        System.out.println(id);
        User user = userService.selectById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(user);
        jsonResult.getMap();
        return jsonResult.getMap();

    }

    @PostMapping("/insert")
    public Map insert(@RequestBody User user) {
        Integer insert = userService.insert(user);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(insert);
        return jsonResult.getMap();

    }

    /**当需要使用HttpSession的时候,将其声明在方法的参数中,
     * Spring:MVC会自动注入该对象:
     *HttpSession session= request.getSession
     * 当参数声明的时候,就从容器中找到bean并自动注入(byType)
     *  param :用query
     *  body:  用body
     * @param phone
     * @param pwd
     * @param session
     */
    @ApiOperation(value = "登录的方法",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone",value = "手机号",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "pwd",value = "用户密码",dataType = "string",paramType = "query")
    })
    @PostMapping(value = "/login")
    public  Map login( String phone, String pwd, @ApiIgnore HttpSession session){
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        session.setAttribute("token",token);
        JsonResult jsonResult =new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("登陆成功");
        jsonResult.set("token",token);
     return    jsonResult.getMap();
    }

    @PostMapping(value = "/reg")
    public  Map reg(User user){
        System.out.println("请求来了");
        user.setId(11);

        JsonResult jsonResult =new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("cg");
        jsonResult.setData(user);
        return  jsonResult.getMap();
    }

    /**
     * 参数是对象类型,
     * Swagger使用body进行描述，测试时，需使用json字符串形式进行测试
     * Swagger的参数使用dataType，会使用User的json形式进行测试
     * @param user
     * @return
     */
    @ApiOperation("用户修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user",value = "修改的用户信息",paramType = "body",dataType = "User")
    })
    public Map update(@RequestBody User user){
        System.out. println(user) ;
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode ("200") ;
        return jsonResult.getMap() ;
    }
}
